//
//  ViewController.m
//  SideDemo
//
//  Created by zsp on 16/4/8.
//  Copyright © 2016年 zsp. All rights reserved.
//

#import "ViewController.h"

#import <MMDrawerController.h>

#import "CenterVC.h"
#import "LeftVC.h"

#import "WildDogService.h"

#ifndef ITEM_WIDTH
#define ITEM_WIDTH 80
#define ITEM_MARGEN 30
#endif

@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong)UICollectionView *dCollectionView;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self configView];
    [self saveData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -
- (void)saveData {
//    [[WildDogService shareInstance]saveData:@"test"];
//    [[WildDogService shareInstance]updateData:@"test"];
    [[WildDogService shareInstance]postsData:@"test"];
    
    
}

#pragma mark -
- (void)configView {
    
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc]init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    CGSize size=[UIScreen mainScreen].bounds.size;
    
    //配合使用
    CGFloat space = floor((size.width - 4*ITEM_WIDTH - 2*ITEM_MARGEN)/3);
    flowLayout.minimumLineSpacing = space;
    flowLayout.minimumInteritemSpacing = space;
    
    
    self.dCollectionView=[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    self.dCollectionView.dataSource=self;
    self.dCollectionView.delegate=self;
    self.dCollectionView.pagingEnabled=YES;
    [self.dCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"dCell"];
    
    
    [self.view addSubview:self.dCollectionView];
    
    UIButton *infoButton=[UIButton buttonWithType:UIButtonTypeContactAdd];
    infoButton.center=self.view.center;
    [infoButton addTarget:self action:@selector(toOpenNewVC:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:infoButton];
}

- (void)toOpenNewVC:(UIButton*)sender {
    CenterVC *centerVC=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CenterVC"];
    centerVC.title=@"Center";
    LeftVC *leftVC=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LeftVC"];
    leftVC.title=@"LeftVC";
    UINavigationController *centerNav=[[UINavigationController alloc]initWithRootViewController:centerVC];
    centerNav.navigationBar.translucent=NO;
    MMDrawerController *drawerVC=[[MMDrawerController alloc]initWithCenterViewController:centerNav leftDrawerViewController:[[UINavigationController alloc]initWithRootViewController:leftVC]];
    [drawerVC setShowsShadow:NO];
    [drawerVC setRestorationIdentifier:@"MMDrawer"];
    [drawerVC setMaximumRightDrawerWidth:200.0];
    [drawerVC setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    [drawerVC setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    
    [self presentViewController:drawerVC animated:YES completion:nil];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 140;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"dCell" forIndexPath:indexPath];
    cell.backgroundColor=[UIColor grayColor];
    return cell;
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(ITEM_WIDTH, ITEM_WIDTH);//配合使用
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(ITEM_MARGEN, ITEM_MARGEN, ITEM_MARGEN, ITEM_MARGEN);//配合使用
}

#pragma mark -
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
//    CGFloat pageWidth = scrollView.bounds.size.width;
//    float fractionalPage = scrollView.contentOffset.x / pageWidth;
        CGFloat pageWidth = scrollView.bounds.size.height;
        float fractionalPage = scrollView.contentOffset.y / pageWidth;
    NSInteger index = lround(fractionalPage);
    NSLog(@"index = %ld",(long)index);
}

@end
